<?php
/**
 * Created by PhpStorm.
 * User: dubin
 * Date: 2016/12/18
 * Time: 13:26
 */

namespace App\Repositories\Eloquent;

use App\Repositories\Contracts\UserInterface;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Container\Container as App;

abstract class Repository implements UserInterface
{

    /*App容器*/
    protected $app;

    /*操作model*/
    protected $model;

    public function __construct(App $app)
    {
        $this->app = $app;
        $this->makeModel();
    }

    abstract function model();

    function findBy($id)
    {
        return $this->model->find($id);
    }

    public function makeModel()
    {
        $model = $this->app->make($this->model());
        /*是否是Model实例*/
        if (!$model instanceof Model) {
            throw new RepositoryException("Class {$this->model()} must be an instance of Illuminate\\Database\\Eloquent\\Model");
        }
        $this->model = $model;
    }

    public function all($columns = ['*']){
        return $this->model->all($columns);
    }

    public function create(array $attributes)
    {
        $model = new $this->model;
        return $model->fill($attributes)->save();
    }

    public function findByField($field, $value, $columns = ['*']){
        return $this->model->select($columns)->where($field,$value)->get();
    }

}